﻿/* 
 *  <copyright file="UnitsConversion.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using Microsoft.Xna.Framework;

namespace SobrietyEngine.Physics
{
    /// <summary>
    /// Convert units between display and simulation units.
    /// </summary>
    public class ConvertUnits
    {
        private float displayToSimUnitsRatio;
        public float DisplayToSimUnitsRatio { get{ return displayToSimUnitsRatio; } set{} }

        private float simToDisplayUnitsRatio;
        public float SimToDisplayUnitsRatio { get{ return simToDisplayUnitsRatio; } set{} }

        public ConvertUnits()
        {
            displayToSimUnitsRatio = 100f;
            simToDisplayUnitsRatio = 1 / displayToSimUnitsRatio;
        }

        public void SetDisplayUnitToSimUnitRatio(float displayUnitsPerSimUnit)
        {
            displayToSimUnitsRatio = displayUnitsPerSimUnit;
            simToDisplayUnitsRatio = 1 / displayUnitsPerSimUnit;
        }

        public float ToDisplayUnits(float simUnits)
        {
            return simUnits * displayToSimUnitsRatio;
        }

        public float ToDisplayUnits(int simUnits)
        {
            return simUnits * displayToSimUnitsRatio;
        }

        public Vector2 ToDisplayUnits(Vector2 simUnits)
        {
            return simUnits * displayToSimUnitsRatio;
        }

        public void ToDisplayUnits(ref Vector2 simUnits, out Vector2 displayUnits)
        {
            Vector2.Multiply(ref simUnits, displayToSimUnitsRatio, out displayUnits);
        }

        public Vector3 ToDisplayUnits(Vector3 simUnits)
        {
            return simUnits * displayToSimUnitsRatio;
        }

        public Vector2 ToDisplayUnits(float x, float y)
        {
            return new Vector2(x, y) * displayToSimUnitsRatio;
        }

        public void ToDisplayUnits(float x, float y, out Vector2 displayUnits)
        {
            displayUnits = Vector2.Zero;
            displayUnits.X = x * displayToSimUnitsRatio;
            displayUnits.Y = y * displayToSimUnitsRatio;
        }

        public float ToSimUnits(float displayUnits)
        {
            return displayUnits * simToDisplayUnitsRatio;
        }

        public float ToSimUnits(double displayUnits)
        {
            return (float)displayUnits * simToDisplayUnitsRatio;
        }

        public float ToSimUnits(int displayUnits)
        {
            return displayUnits * simToDisplayUnitsRatio;
        }

        public Vector2 ToSimUnits(Vector2 displayUnits)
        {
            return displayUnits * simToDisplayUnitsRatio;
        }

        public Vector3 ToSimUnits(Vector3 displayUnits)
        {
            return displayUnits * simToDisplayUnitsRatio;
        }

        public void ToSimUnits(ref Vector2 displayUnits, out Vector2 simUnits)
        {
            Vector2.Multiply(ref displayUnits, simToDisplayUnitsRatio, out simUnits);
        }

        public Vector2 ToSimUnits(float x, float y)
        {
            return new Vector2(x, y) * simToDisplayUnitsRatio;
        }

        public Vector2 ToSimUnits(double x, double y)
        {
            return new Vector2((float)x, (float)y) * simToDisplayUnitsRatio;
        }

        public void ToSimUnits(float x, float y, out Vector2 simUnits)
        {
            simUnits = Vector2.Zero;
            simUnits.X = x * simToDisplayUnitsRatio;
            simUnits.Y = y * simToDisplayUnitsRatio;
        }
    }
}